package com.wzq.app.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wzq.app.R;
import com.wzq.app.api.Api;
import com.wzq.app.api.ApiResponseCode;
import com.wzq.app.base.BaseActivity;
import com.wzq.app.constant.RequestCode;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.BaseModel;
import com.wzq.app.model.CommentModel;
import com.wzq.app.model.TeachEveModel;
import com.wzq.app.utils.ResponseCode;
import com.wzq.app.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: 2021WZQ
 * @Package: com.wzq.app.ui
 * @ClassName: MyFeedBackActivity
 * @Description: java类作用描述
 * @Author: Tao.xiaolong
 * @CreateDate: 2021-04-06 22:30
 * @UpdateUser: 更新者：
 * @UpdateDate: 2021-04-06 22:30
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MyFeedBackActivity extends BaseActivity {
    private static final String TAG = "MyFeedBackActivity";

    @BindView(R.id.head_left)
    TextView headLeft;
    @BindView(R.id.head_left_text)
    TextView headLeftText;
    @BindView(R.id.head_right_text)
    TextView headRightText;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.content)
    TextView content;

    List<TeachEveModel> teachEveModels;
    List<CommentModel> commentModels;

    @Override
    public int getLayoutId() {
        return R.layout.feedback_activity;
    }

    @Override
    public void configView() {
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void requestData() {
        Call<BaseModel<List<TeachEveModel>>> call = Api.getInstance().service.getEvaByUserId(UserManager.getInstance().getUser().getId());
        call.enqueue(new Callback<BaseModel<List<TeachEveModel>>>() {
            @Override
            public void onResponse(Call<BaseModel<List<TeachEveModel>>> call, Response<BaseModel<List<TeachEveModel>>> response) {
                if (response.isSuccessful()) {
                    BaseModel<List<TeachEveModel>> baseModel = response.body();
                    if (baseModel.code == ApiResponseCode.RESPHONE_OK) {
                        teachEveModels = baseModel.obj;
                        getComment();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<List<TeachEveModel>>> call, Throwable t) {
                ToastUtils.showToast(mContext, "查询失败，请检查网络");
            }
        });
    }

    private void getComment() {
        Call<BaseModel<List<CommentModel>>> call = Api.getInstance().service.getComment();
        call.enqueue(new Callback<BaseModel<List<CommentModel>>>() {
            @Override
            public void onResponse(Call<BaseModel<List<CommentModel>>> call, Response<BaseModel<List<CommentModel>>> response) {
                if (response.isSuccessful()) {
                    BaseModel<List<CommentModel>> baseModel = response.body();
                    Log.d(TAG, "getComment:" + response.toString());
                    if (baseModel.code == ApiResponseCode.RESPHONE_OK) {
                        commentModels = baseModel.obj;
                        updateUI();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<List<CommentModel>>> call, Throwable t) {
                ToastUtils.showToast(mContext, "查询失败，请检查网络");
            }
        });
    }

    private void updateUI() {
        String goal = teachEveModels.get(0).getGoal();
        number.setText(goal + "分");
        double g = Double.valueOf(goal);
        for (CommentModel model : commentModels) {
            Log.d(TAG, "CommentModel:" + model.toString());
            if (g >= model.getGoal()) {
                content.setText(model.getComment());
                break;
            }
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
